package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.exception.FdfsServerException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.dubbo.api.PeachblossomApi;
import com.tanhua.dubbo.api.VoiceApi;
import com.tanhua.model.db.Analysis;
import com.tanhua.model.mongo.Voice;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: tanhua
 * @description:
 * @author: MR.peng
 * @create: 2022-03-31 19:33
 **/
@Service
public class AnalysisService {

        @Autowired
        private LogMapper logMapper;

        @Autowired
        private AnalysisMapper  analysisMapper;

        @Autowired
        private FdfsWebServer server;

        @Autowired
        private FastFileStorageClient client;


    /*
    * 计算日志表中的数据，保存或者更新到统计表
    * */
    public void analysis() throws ParseException {
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String yesterday = DateUtil.yesterday().toString("yyyy-MM-dd");
        //1.查询数据
        //1.1 新注册用户数
        Integer regNum = logMapper.queryByLogTimeAndType("0102", today);
        //1.2 活跃用户数
        Integer actNum = logMapper.queryByLogTime(today);
        //1.3 登陆次数
        Integer logNum = logMapper.queryByLogTimeAndType("0101", today);
        //1.4 次日留存用户数
        Integer retNum = logMapper.queryNumRetention(today, yesterday);

        //2.根据统计日期查询数据
        QueryWrapper<Analysis> qw = new QueryWrapper<>();
        qw.eq("record_date",today);
        Analysis analysis = analysisMapper.selectOne(qw);
        //3.判断数据是否存在
        if(analysis == null){
            //2.1数据不存在 ，保存
            analysis = new Analysis();
            analysis.setNumActive(actNum);
            analysis.setNumRegistered(regNum);
            analysis.setNumLogin(logNum);
            analysis.setNumRetention1d(retNum);
            analysis.setCreated(new Date());
            analysis.setRecordDate(new SimpleDateFormat("yyyy-MM-dd").parse(today));//保存的日期
            analysisMapper.insert(analysis);
        }else {
            //2.2数据存在，更新
            analysis.setNumActive(actNum);
            analysis.setNumRegistered(regNum);
            analysis.setNumLogin(logNum);
            analysis.setNumRetention1d(retNum);
            analysisMapper.updateById(analysis);

        }

    }


    @DubboReference
    private PeachblossomApi peachblossomApi;

    @DubboReference
    private VoiceApi voiceApi;
    //恢复次数
    public void regain() {
        peachblossomApi.updateRemainingTimes(10);
    }

    //删除以拾取的语音
    public void delVoice() {
        List<Voice> list = voiceApi.delVoice();
        String webServerUrl = server.getWebServerUrl();
        if (CollUtil.isNotEmpty(list)) {
            for (Voice voice : list) {
                String soundUrl = voice.getSoundUrl();
                String s = soundUrl.replaceAll(webServerUrl, "");
                //删除文件
                try{
                    client.deleteFile(s);
                } catch (FdfsServerException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
